package com.ssl.basics.thread;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：这里写了一个两个线程互相获取对方手上的资源 而产生的死锁现象，查看死锁：
 * jps -m 查看当前进程ID
 * jstack 进程ID ：可以看到当前进程执行的快照
 *
 * @author shi_senlin
 * @create 2021/07/10 13:02
 */
@Slf4j(topic = "com.ssl.thread")
public class ThreadDeadlock {
    static Object o1 = new Object();
    static Object o2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            synchronized (o1) {
                log.info("{} 持有{}锁，正在尝试获取{}锁", Thread.currentThread().getName(), "o1", "02");

                synchronized (o2) {
                    log.info("获取到 {} 锁了。", "o2");
                }
            }

        },"t1").start();

        new Thread(() -> {
            synchronized (o2) {
                log.info("{} 持有{}锁，正在尝试获取{}锁", Thread.currentThread().getName(), "o2", "01");
                synchronized (o1) {
                    log.info("获取到 {} 锁了。", "o2");
                }
            }

        },"t2").start();
    }
}
